package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;

import static org.springframework.amqp.rabbit.annotation.Exchange.TRUE;

/**
 * Package: com.atguigu.tingshu.search.receiver
 *
 * @author xiongkunYang
 * @description:
 * @since 2025/6/19 14:26
 */
@Slf4j
@Component
public class SearchReceiver {

    private final SearchService searchService;

    public SearchReceiver(SearchService searchService) {
        this.searchService = searchService;
    }

    /**
     * 监听专辑上架消息，专辑上架成功后，将专辑信息保存到索引库
     *
     * @param albumId 专辑id
     * @param message 消息
     * @param channel 通道
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER, durable = TRUE),
            key = {MqConst.ROUTING_ALBUM_UPPER}
    ))
    public void upperAlbum(Long albumId, Message message, Channel channel) {
        Assert.notNull(albumId, "专辑id不能为空");
        log.info("[搜索服务]监听专辑上架消息，专辑id：{}", albumId);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            this.searchService.upperAlbum(albumId);
            channel.basicAck(deliveryTag, false);
            log.info("[搜索服务]专辑id：{}, 上架成功", albumId);
        } catch (IOException e) {
            log.error("[搜索服务]专辑id：{}, 上架失败", albumId);
            channel.basicNack(deliveryTag, false, true);
        }

    }

    /**
     * 监听专辑下架消息，专辑下架成功后，将专辑信息从索引库删除
     *
     * @param albumId 专辑id
     * @param message 消息
     * @param channel 通道
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER, durable = TRUE),
            key = {MqConst.ROUTING_ALBUM_LOWER}
    ))
    public void lowerAlbum(Long albumId, Message message, Channel channel) {
        Assert.notNull(albumId, "专辑id不能为空");
        log.info("[搜索服务]监听专辑下架消息，专辑id：{}", albumId);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            this.searchService.lowerAlbum(albumId);
            channel.basicAck(deliveryTag, false);
            log.info("[搜索服务]专辑id：{}, 下架成功", albumId);
        } catch (IOException e) {
            log.error("[搜索服务]专辑id：{}, 下架失败", albumId);
            channel.basicNack(deliveryTag, false, true);
        }
    }
}